package com.alstudio.ui.base;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alstudio.R;
import com.alstudio.utils.log.Mylog;
import com.alstudio.view.listener.AnimationEndListener;
import com.alstudio.view.listener.ViewEventListener;

public abstract class TitleAndBottomBarActivity extends BaseActivity {

	// 状态栏布局
	private LinearLayout titleBarLayout, customTitleBarLayout;
	// 内容布局
	private LinearLayout mContentLayout, bottomContentLayout;
	private RelativeLayout bottomBarLayout;
	// 本类界面独有的通知区域。显示在状态栏下发
	private LinearLayout titleBarNoticeArea;

	private View baseTitleBarLayout;

	// 中间文字信息
	private TextView center_msg;

	// 左边按钮
	private Button leftButton;
	// 右边按钮
	private Button rightButton;
	// 左边image button
	private ImageButton leftImgButton;
	// 右边image button
	private ImageButton rightImgButton;
	// 自定义左按键区
	private LinearLayout mLeftCustomButton;
	// 自定义右按键区
	private LinearLayout mRightCustomButton;
	// 左边按钮类型
	private ButtonType mLeftButtonType = ButtonType.BUTTON_NORMAL;
	// 右边按钮类型
	private ButtonType mRightButtonType = ButtonType.BUTTON_NORMAL;

	// 状态栏通知区域事件监听器
	private NoticeAreaEventListener mTitleBarNoticeAreaListener;
	// 此变量用于标识当前是否正在显示状态栏通知区域
	private boolean isTitleBarNoticeAreaShowing = false;
	// 此变量用于标识是否需要定时自动关闭状态栏通知区域
	private boolean isAutoDismissTitleBarNoticeAreaEnable = true;
	// 状态栏通知区域缺省显示时常
	private long TITLE_BAR_NOTICE_AREA_SHOW_DURATION = 5 * 1000;
	// 状态栏通知区域显示时常
	private long mNoticeAreaShowDuration = TITLE_BAR_NOTICE_AREA_SHOW_DURATION;
	// 自定义状态栏通知区域显示动画资源
	private int mTitleBarNoticeAreaShowAnimationRes = -1;
	// 自定义状态栏通知区域隐藏动画资源
	private int mTitleBarNoticeAreaDismissAnimationRes = -1;
	// 状态栏通知区域的显示、隐藏动画
	private Animation titleBarNoticeAnimation;
	// 关闭状态栏通知区域
	private final int DISMISS_TITLE_BAR_NOTICE_AREA = 10012;

	// 底栏显示出来的动画
	private int mBottomBarShowAnimationRes = -1;
	// 底栏消失时的动画
	private int mBottomBarDismissAnimationRes = -1;
	// 底栏定时自动消失
	private boolean isAutoDismissBottomBar = true;
	// 关闭底栏
	private final int DISMISS_BOTTOM_BAR = 10015;
	// 底栏的显示、隐藏动画
	private Animation bottomBarAnimation;
	// 底栏事件监听器
	private BottomBarEventListener mBottomBarEventListener;
	// 底栏通知区域缺省显示时常
	private long BOTTOM_BAR_SHOW_DURATION = 5 * 1000;
	// 状态栏通知区域显示时常
	private long mBottomBarShowDuration = BOTTOM_BAR_SHOW_DURATION;

	private boolean isBottomBarShowing = true;

	private boolean isLeftButtonShow = true;
	private boolean isRightButtonShow = true;

	// 调试TAG
	private final String TAG = "TitleAndBottomBarActivity";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 初始化UI控件
		initWidget();
	}

	/**
	 * 初始化UI控件
	 * 
	 * @author Alonso Lee
	 */
	private void initWidget() {
		// 将布局文件加入到BaseActivity中
		setContentRes(R.layout.base_title_and_buttom_bar_layout);
		// 状态栏布局
		titleBarLayout = (LinearLayout) findViewById(R.id.title_bar);
		// 底部布局
		bottomBarLayout = (RelativeLayout) findViewById(R.id.buttom_bar);
		// 底部内容区
		bottomContentLayout = (LinearLayout) findViewById(R.id.buttom_content_layout);
		// 自定义状态栏
		customTitleBarLayout = (LinearLayout) findViewById(R.id.custom_title_bar);
		// 主内容区
		mContentLayout = (LinearLayout) findViewById(R.id.center_content_layout);

		// // 缺省状态栏
		// baseTitleBarLayout = getLayoutInflater().inflate(
		// R.layout.base_title_bar, null);

		baseTitleBarLayout = findViewById(R.id.base_mini_title_bar);

		// 中间文字信息
		center_msg = (TextView) findViewById(R.id.center_msg);

		// 左按钮
		leftButton = (Button) findViewById(R.id.button_left);
		// 右按钮
		rightButton = (Button) findViewById(R.id.button_right);
		// 左边image 按钮
		leftImgButton = (ImageButton) findViewById(R.id.img_button_left);
		// 右边 image 按钮
		rightImgButton = (ImageButton) findViewById(R.id.img_button_right);
		// 左边自定义按键
		mLeftCustomButton = (LinearLayout) findViewById(R.id.custom_button_left);
		// 右边自定义按键
		mRightCustomButton = (LinearLayout) findViewById(R.id.custom_button_right);

		// title bar下的通知区域
		titleBarNoticeArea = (LinearLayout) findViewById(R.id.title_bar_notice_area);

		// 设置缺省的点击事件
		setButtonDefaultClickListener();
	}

	/**
	 * 设置TietleBar标题栏下面内容区的布局文件
	 * 
	 * @param layoutResId
	 *            布局资源文件
	 * @author Alonso Lee
	 */
	protected void setTitleBarContentView(int layoutResId) {
		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(layoutResId, null);
		if (v != null) {
			v.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			mContentLayout.addView(v);
		}
	}

	/**
	 * 设定底部区域的布局
	 * 
	 * @param layoutRes
	 * @author Alonso Lee
	 */
	protected void setButtomContentView(int layoutRes) {

		LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		View v = inflater.inflate(layoutRes, null);
		if (v != null) {
			v.setLayoutParams(new LinearLayout.LayoutParams(
					LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
			bottomContentLayout.addView(v);
		}
	}

	/**
	 * 设定左边按钮类型
	 * 
	 * @param type
	 */
	protected void setLeftButtonType(ButtonType type) {
		this.mLeftButtonType = type;
		updateButtonConfig(true);
	}

	/**
	 * 设定右边按钮类型
	 * 
	 * @param type
	 */
	protected void setRightButtonType(ButtonType type) {
		this.mRightButtonType = type;
		updateButtonConfig(false);
	}

	/**
	 * 获取左边按钮类型
	 * 
	 * @return
	 */
	protected ButtonType getLeftButtonType() {
		return this.mLeftButtonType;
	}

	/**
	 * 获取右边按钮类型
	 * 
	 * @return
	 */
	protected ButtonType getRightButtonType() {
		return this.mRightButtonType;
	}

	/**
	 * 获取左边Button控件
	 * 
	 * @return Button控件实例
	 * @author Alonso Lee
	 */
	public Button getLeftButton() {
		return this.leftButton;
	}

	/**
	 * 获取左边ImageButton控件
	 * 
	 * @return ImageButton控件实例
	 * @author Alonso Lee
	 */
	public ImageButton getLeftImageButton() {
		return this.leftImgButton;
	}

	/**
	 * 获取左边自定义按钮控件
	 * 
	 * @return 左边自定义按钮控件
	 * @author Alonso Lee
	 */
	public LinearLayout getLeftCustomButton() {
		return this.mLeftCustomButton;
	}

	/**
	 * 获取右边Button控件
	 * 
	 * @return Button控件实例
	 * @author Alonso Lee
	 */
	public Button getRightButton() {
		return this.rightButton;
	}

	/**
	 * 获取右边ImageButton控件
	 * 
	 * @return ImageButton控件实例
	 * @author Alonso Lee
	 */
	public ImageButton getRightImageButton() {
		return this.rightImgButton;
	}

	/**
	 * 获取右边自定义按钮控件
	 * 
	 * @return 右边自定义按钮控件
	 * @author Alonso Lee
	 */
	public LinearLayout getRightCustomButton() {
		return this.mRightCustomButton;
	}

	/**
	 * 获取中间TextView对象
	 * 
	 * @return 中间TextView对象实例
	 * @author Alonso Lee
	 */
	public TextView getCenterTextView() {
		return this.center_msg;
	}

	/**
	 * 设定左边按钮文字信息
	 * 
	 * @param name
	 *            要显示的文字信息
	 * @author Alonso Lee
	 */
	public void setLeftButtonLable(String name) {
		this.leftButton.setText(name);
	}

	public void setLeftButtonLableRes(int res) {
		this.leftButton.setText(res);
	}

	/**
	 * 设置右边按钮文字信息
	 * 
	 * @param name
	 *            文字信息
	 * @author Alonso Lee
	 */
	public void setRightButtonLable(String name) {
		this.rightButton.setText(name);
	}

	public void setRightButtonLableRes(int res) {
		this.rightButton.setText(res);
	}

	/**
	 * 设定导航栏中间文字信息
	 * 
	 * @param msg
	 * @author Alonso Lee
	 */
	public void setCenterTextViewMessage(String msg) {
		this.center_msg.setText(msg);
	}

	/**
	 * 设定导航栏中间文字信息
	 * 
	 * @param res
	 *            文字资源
	 */
	public void setCenterTextViewMessage(int res) {
		this.center_msg.setText(res);
	}

	/**
	 * 设定导航栏左边按钮资源图片
	 * 
	 * @param resId
	 *            资源图片id
	 * @author Alonso Lee
	 */
	public void setLeftButtonBackground(int resid) {
		setButtonBackground(true, resid);
	}

	/**
	 * 设定导航栏右边按钮资源图片
	 * 
	 * @param resid
	 *            资源图片id
	 * @author Alonso Lee
	 */
	public void setRightButtonBackground(int resid) {
		setButtonBackground(false, resid);
	}

	/**
	 * 设定导航栏左边按钮资源图片
	 * 
	 * @param background
	 *            资源图片
	 * @author Alonso Lee
	 */
	public void setLeftButtonBackground(Drawable background) {
		setButtonBackground(true, background);
	}

	/**
	 * 设定导航栏右边按钮资源图片
	 * 
	 * @param background
	 *            资源图片
	 * @author Alonso Lee
	 */
	public void setRightButtonBackground(Drawable background) {
		setButtonBackground(false, background);
	}

	/**
	 * 设定导航栏左边按钮文字颜色。仅仅左边按钮类型为BUTTON_NORMAL时有效
	 * 
	 * @param color
	 *            文字颜色
	 * @author Alonso Lee
	 */
	public void setLeftButtonTextColor(int color) {
		this.leftButton.setTextColor(color);
	}

	/**
	 * 设定导航栏右边按钮文字颜色。仅仅左边按钮类型为BUTTON_NORMAL时有效
	 * 
	 * @param color
	 *            文字颜色
	 * @author Alonso Lee
	 */
	public void setRightButtonTextColor(int color) {
		this.rightButton.setTextColor(color);
	}

	/**
	 * 设定导航栏中间文字颜色
	 * 
	 * @param color
	 * @author Alonso Lee
	 */
	public void setCenterTextViewTextColor(int color) {
		this.center_msg.setTextColor(color);
	}

	/**
	 * 隐藏、显示缺省状态栏
	 * 
	 * @param hide
	 */
	public void hideTitleBar(boolean hide) {
		if (hide) {
			this.baseTitleBarLayout.setVisibility(View.GONE);
		} else {
			this.baseTitleBarLayout.setVisibility(View.VISIBLE);
		}
	}

	public boolean isTitleBarHidden() {
		switch (baseTitleBarLayout.getVisibility()) {
		case View.VISIBLE:
			return false;
		case View.GONE:
		case View.INVISIBLE:
			return true;
		default:
			return false;
		}
	}

	/**
	 * 设定左边按钮显示状态
	 * 
	 * @param hide
	 *            true 表示隐藏左边按钮，false表示显示左边按钮
	 * @author Alonso Lee
	 */
	public void hideLeftButton(boolean hide) {
		isLeftButtonShow = hide;
		if (hide) {
			leftButton.setVisibility(View.INVISIBLE);
			leftImgButton.setVisibility(View.INVISIBLE);
			mLeftCustomButton.setVisibility(View.INVISIBLE);
		} else {
			leftButton.setVisibility(View.VISIBLE);
			leftImgButton.setVisibility(View.VISIBLE);
			mLeftCustomButton.setVisibility(View.INVISIBLE);
		}
	}

	/**
	 * 测试左边按钮是否已经被隐藏
	 * 
	 * @return true 表示左边按钮已经被隐藏，false表示左边按钮当前处于可见状态
	 * @author Alonso Lee
	 */
	public boolean isLeftButtonHidden() {
		return isLeftButtonShow;
	}

	/**
	 * 设定右边按钮显示状态
	 * 
	 * @param hide
	 *            true 表示隐藏右边按钮，false表示显示右边按钮
	 * @author Alonso Lee
	 */
	public void hideRightButton(boolean hide) {
		isRightButtonShow = hide;
		if (hide) {
			rightButton.setVisibility(View.INVISIBLE);
			rightImgButton.setVisibility(View.INVISIBLE);
			mRightCustomButton.setVisibility(View.INVISIBLE);
		} else {
			rightButton.setVisibility(View.VISIBLE);
			rightImgButton.setVisibility(View.VISIBLE);
			mRightCustomButton.setVisibility(View.INVISIBLE);
		}
	}

	/**
	 * 测试右边按钮是否已经被隐藏
	 * 
	 * @return true 表示右边按钮已经被隐藏，false表示右边按钮当前处于可见状态
	 * @author Alonso Lee
	 */
	public boolean isRightButtonHidden() {
		return isRightButtonShow;
	}

	/**
	 * 设定状态栏左边按钮事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setLeftButtonClickListener(View.OnClickListener listener) {
		if (listener != null) {
			leftButton.setOnClickListener(listener);
			leftImgButton.setOnClickListener(listener);
			mLeftCustomButton.setOnClickListener(listener);
		} else {
			setButtonDefaultClickListener();
		}
	}

	/**
	 * 设定状态栏右边按钮事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	public void setRightButtonClickListener(View.OnClickListener listener) {
		rightButton.setOnClickListener(listener);
		rightImgButton.setOnClickListener(listener);
		mRightCustomButton.setOnClickListener(listener);
	}

	/**
	 * 设定状态栏通知区域view事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaListener(
			NoticeAreaEventListener listener) {
		if (listener != null) {
			mTitleBarNoticeAreaListener = listener;
		}
	}

	/**
	 * 移除状态栏通知区域view事件监听器
	 * 
	 * @author Alonso Lee
	 */
	protected void removeTitleBarNoticeAreaListener() {
		mTitleBarNoticeAreaListener = null;
	}

	/**
	 * 将制定的view添加到状态栏通知区域中。
	 * 
	 * @param child
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeLayout(View child) {

		if ((titleBarNoticeArea != null) && (child != null)) {
			titleBarNoticeArea.removeAllViews();
			titleBarNoticeArea.addView(child);
		}
	}

	/**
	 * 设定状态栏通知区域显示时常。传入的值为毫秒。
	 * 
	 * @param time
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaShowDuration(long ms) {
		if (ms > 0) {
			mNoticeAreaShowDuration = ms;
		}
	}

	/**
	 * 设定状态栏通知区域显示时的过场动画
	 * 
	 * @param res
	 *            -1则表示恢复缺省动画
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaShowAnimationRes(int res) {
		mTitleBarNoticeAreaShowAnimationRes = res;
	}

	/**
	 * 设定状态栏通知区域隐藏时的过场动画
	 * 
	 * @param res
	 *            -1则表示恢复缺省动画
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaDismissAnimationRes(int res) {
		mTitleBarNoticeAreaDismissAnimationRes = res;
	}

	/**
	 * 设定状态栏通知区域是否定时自动消失
	 * 
	 * @param enable
	 *            true表示需要自动消失 false 表示不需要自动消失
	 * @author Alonso Lee
	 */
	protected void setTitleBarNoticeAreaAutoDismiss(boolean enable) {
		isAutoDismissTitleBarNoticeAreaEnable = enable;
	}

	/**
	 * 检查状态栏通知区域当前是否设为定时自动消失
	 * 
	 * @return true 表示当前设定为自动消失
	 * @author Alonso Lee
	 */
	protected boolean isTitleBarNoticeAreaAutoDismiss() {
		return isAutoDismissTitleBarNoticeAreaEnable;
	}

	/**
	 * 显示状态栏通知区域
	 * 
	 * @author Alonso Lee
	 */
	protected void showTitleBarNotice() {
		if (titleBarNoticeArea != null) {
			titleBarNoticeArea.setVisibility(View.VISIBLE);
			isTitleBarNoticeAreaShowing = true;
			if (mTitleBarNoticeAreaShowAnimationRes != -1) {
				// 设定自定义显示动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						mTitleBarNoticeAreaShowAnimationRes);
			} else {
				// 使用缺省显示动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						R.anim.activity_down);
			}
			if (titleBarNoticeAnimation != null) {
				titleBarNoticeAnimation
						.setAnimationListener(titleBarNoticeAreaAnimationListener);
				titleBarNoticeArea.startAnimation(titleBarNoticeAnimation);
			} else {
				Mylog.e("尼玛，传了个无效的动画给我!");
			}
		}
	}

	/**
	 * 隐藏状态栏通知区域
	 * 
	 * @author Alonso Lee
	 */
	protected void dismissTitleBarNotice() {
		titleBarNoticeAreaHandler.removeMessages(DISMISS_TITLE_BAR_NOTICE_AREA);
		if (titleBarNoticeArea != null) {
			isTitleBarNoticeAreaShowing = false;

			if (mTitleBarNoticeAreaDismissAnimationRes != -1) {
				// 设定自定义消失动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						mTitleBarNoticeAreaDismissAnimationRes);
			} else {
				// 使用缺省显示动画
				titleBarNoticeAnimation = AnimationUtils.loadAnimation(this,
						R.anim.activity_up);
			}
			if (titleBarNoticeAnimation != null) {
				titleBarNoticeAnimation
						.setAnimationListener(titleBarNoticeAreaAnimationListener);
				titleBarNoticeArea.startAnimation(titleBarNoticeAnimation);
			}
		}
	}

	/**
	 * 此handler用于控制定时关闭状态栏通知区域
	 * 
	 * @author Alonso Lee
	 */
	private Handler titleBarNoticeAreaHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			dismissTitleBarNotice();
		};
	};

	/**
	 * 监听状态栏通知区域动画结束
	 * 
	 * @author Alonso Lee
	 */
	private AnimationListener titleBarNoticeAreaAnimationListener = new AnimationEndListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			if (isTitleBarNoticeAreaShowing) {
				// 回调通知，已经正常显示了
				if (mTitleBarNoticeAreaListener != null) {
					mTitleBarNoticeAreaListener.onShow();
				}
				// 显示通知区域动画显示完毕，如果开启了定时自动关闭通知区域，那么延时通过handler实现
				if (isAutoDismissTitleBarNoticeAreaEnable) {
					titleBarNoticeAreaHandler.sendEmptyMessageDelayed(
							DISMISS_TITLE_BAR_NOTICE_AREA,
							mNoticeAreaShowDuration);
				}

			} else {
				// 隐藏通知区域
				titleBarNoticeArea.setVisibility(View.GONE);
				// 回调通知，已经隐藏了
				if (mTitleBarNoticeAreaListener != null) {
					mTitleBarNoticeAreaListener.onDismiss();
				}
			}
		}
	};

	/**
	 * 设定底栏显示的时长
	 * 
	 * @param ms
	 * @author Alonso Lee
	 */
	protected void setBottomBarShowDuration(long ms) {
		if (ms > 0) {
			mBottomBarShowDuration = ms;
		}
	}

	/**
	 * 获取底栏显示的时长
	 * 
	 * @return
	 * @author Alonso Lee
	 */
	protected long getBottomBarShowDuration() {
		return mBottomBarShowDuration;
	}

	/**
	 * 设定底栏显示时的动画
	 * 
	 * @param res
	 *            -1表示使用缺省动画
	 * @author Alonso Lee
	 */
	protected void setbottomBarShowAnimation(int res) {
		mBottomBarShowAnimationRes = res;
	}

	/**
	 * 设定底栏消失时的动画
	 * 
	 * @param res
	 *            -1表示使用缺省动画
	 * @author Alonso Lee
	 */
	protected void setbottomBarDismissAnimation(int res) {
		mBottomBarDismissAnimationRes = res;
	}

	/**
	 * 设定底栏是否自动消失。
	 * 
	 * @param dismiss
	 *            true 如果设定为true，缺省为5s后自动消失。 false 底栏不自动消失
	 * @author Alonso Lee
	 */
	protected void setBottomBarAutoDismiss(boolean dismiss) {
		isAutoDismissBottomBar = dismiss;
	}

	/**
	 * 查询底栏当前是否设定为自动消失
	 * 
	 * @return true 表示底栏将定时消失
	 * @author Alonso Lee
	 */
	protected boolean isBottomBarAutoDismiss() {
		return isAutoDismissBottomBar;
	}

	/**
	 * 设定底栏消失、显示事件监听器
	 * 
	 * @param listener
	 * @author Alonso Lee
	 */
	protected void setBottomBarEventListener(BottomBarEventListener listener) {
		this.mBottomBarEventListener = listener;
	}

	/**
	 * 显示底栏
	 * 
	 * @author Alonso Lee
	 */
	protected void showBottomBar() {
		bottomBarLayout.setVisibility(View.VISIBLE);
		isBottomBarShowing = true;
		if (mBottomBarShowAnimationRes != -1) {
			// 设定自定义显示动画
			bottomBarAnimation = AnimationUtils.loadAnimation(this,
					mBottomBarShowAnimationRes);
		} else {
			// 使用缺省显示动画
			bottomBarAnimation = AnimationUtils.loadAnimation(this,
					R.anim.buttom_bar_up);
		}
		if (bottomBarAnimation != null) {
			bottomBarAnimation.setAnimationListener(ButtomBarAnimationListener);
			bottomBarLayout.startAnimation(bottomBarAnimation);
		} else {
			Mylog.e("尼玛，传了个无效的动画给我!");
		}

	}

	/**
	 * 隐藏底栏
	 * 
	 * @author Alonso Lee
	 */
	protected void dismissBottomBar() {
		isBottomBarShowing = false;
		titleBarAndButtomHandler.removeMessages(DISMISS_BOTTOM_BAR);
		if (mBottomBarDismissAnimationRes != -1) {
			// 设定自定义显示动画
			bottomBarAnimation = AnimationUtils.loadAnimation(this,
					mBottomBarDismissAnimationRes);
		} else {
			// 使用缺省显示动画
			bottomBarAnimation = AnimationUtils.loadAnimation(this,
					R.anim.buttom_bar_down);
		}
		if (bottomBarAnimation != null) {
			bottomBarAnimation.setAnimationListener(ButtomBarAnimationListener);
			bottomBarLayout.startAnimation(bottomBarAnimation);
		} else {
			Mylog.e("尼玛，传了个无效的动画给我!");
		}

	}

	/**
	 * 监听底栏动画结束
	 * 
	 * @author Alonso Lee
	 */
	private AnimationListener ButtomBarAnimationListener = new AnimationEndListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			proccessButtomBarDisplayState();
		}
	};

	/**
	 * 处理底栏显示状态。通过回调通知UI。
	 * 
	 * @author Alonso Lee
	 */
	private void proccessButtomBarDisplayState() {
		if (isBottomBarShowing) {
			if (isAutoDismissBottomBar) {
				// 如果设定了自动隐藏底栏，那么延时掩藏她
				titleBarAndButtomHandler.sendEmptyMessageDelayed(
						DISMISS_BOTTOM_BAR, mBottomBarShowDuration);
			}
			// 显示动画播放完毕
			if (mBottomBarEventListener != null) {
				mBottomBarEventListener.onShow();
			}
		} else {
			bottomBarLayout.setVisibility(View.GONE);
			// 消失动画播放完毕
			if (mBottomBarEventListener != null) {
				mBottomBarEventListener.onDismiss();
			}
		}

	}

	/**
	 * 此handler用于控制定时关闭状态栏通知区域
	 * 
	 * @author Alonso Lee
	 */
	private Handler titleBarAndButtomHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case DISMISS_BOTTOM_BAR:
				// 自动关闭底栏
				dismissBottomBar();
				break;
			}
		};
	};

	/************************************************************************************* 内部类以及私有方法 ******************************************************************************************************************************************************************************************************************************************/

	private void updateButtonConfig(boolean left) {
		if (left) {
			// 配置左键

			switch (mLeftButtonType) {
			case BUTTON_NORMAL:
				// 显示Button
				leftButton.setVisibility(View.VISIBLE);
				leftImgButton.setVisibility(View.GONE);
				mLeftCustomButton.setVisibility(View.GONE);
				break;
			case IMAGE_BUTTON_NORMAL:
				// 显示ImageButton
				leftImgButton.setVisibility(View.VISIBLE);
				leftButton.setVisibility(View.GONE);
				mLeftCustomButton.setVisibility(View.GONE);
				break;
			case CUSTOM_BUTTON:
				// 显示自定义按键
				mLeftCustomButton.setVisibility(View.VISIBLE);
				leftImgButton.setVisibility(View.GONE);
				leftButton.setVisibility(View.GONE);
				break;
			}

		} else {
			// 配置右键

			switch (mRightButtonType) {
			case BUTTON_NORMAL:
				// 显示Button
				rightButton.setVisibility(View.VISIBLE);
				rightImgButton.setVisibility(View.GONE);
				mRightCustomButton.setVisibility(View.GONE);
				break;
			case IMAGE_BUTTON_NORMAL:
				// 显示ImageButton
				rightImgButton.setVisibility(View.VISIBLE);
				rightButton.setVisibility(View.GONE);
				mRightCustomButton.setVisibility(View.GONE);
				break;
			case CUSTOM_BUTTON:
				// 显示自定义按键
				mRightCustomButton.setVisibility(View.VISIBLE);
				rightImgButton.setVisibility(View.GONE);
				rightButton.setVisibility(View.GONE);
				break;
			}
		}
	}

	private void setButtonBackground(boolean left, int res) {
		if (left) {
			switch (mLeftButtonType) {
			case BUTTON_NORMAL:
				this.leftButton.setBackgroundResource(res);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.leftImgButton.setBackgroundResource(res);
				break;
			case CUSTOM_BUTTON:
				this.mLeftCustomButton.setBackgroundResource(res);
				break;
			}
		} else {
			switch (mRightButtonType) {
			case BUTTON_NORMAL:
				this.rightButton.setBackgroundResource(res);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.rightImgButton.setBackgroundResource(res);
				break;
			case CUSTOM_BUTTON:
				this.mRightCustomButton.setBackgroundResource(res);
				break;
			}
		}
	}

	private void setButtonBackground(boolean left, Drawable background) {
		if (left) {
			switch (mLeftButtonType) {
			case BUTTON_NORMAL:
				this.leftButton.setBackgroundDrawable(background);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.leftImgButton.setBackgroundDrawable(background);
				break;
			case CUSTOM_BUTTON:
				this.mLeftCustomButton.setBackgroundDrawable(background);
				break;
			}
		} else {
			switch (mRightButtonType) {
			case BUTTON_NORMAL:
				this.rightButton.setBackgroundDrawable(background);
				break;
			case IMAGE_BUTTON_NORMAL:
				this.rightImgButton.setBackgroundDrawable(background);
				break;
			case CUSTOM_BUTTON:
				this.mRightCustomButton.setBackgroundDrawable(background);
				break;
			}
		}
	}

	private void setButtonDefaultClickListener() {
		setLeftButtonClickListener(leftButtonDefaultClickListener);
		setRightButtonClickListener(rightButtonDefaultClickListener);
	}

	private OnClickListener leftButtonDefaultClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			finishMyActivity();
		}
	};

	private OnClickListener rightButtonDefaultClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

		}

	};

	/**
	 * 底栏事件监听器
	 * 
	 * @author Alonso Lee
	 * 
	 */
	protected interface BottomBarEventListener extends ViewEventListener {

	}

	/**
	 * 描述按键类型
	 * 
	 * @author Alonso Lee
	 * 
	 */
	protected enum ButtonType {
		// 普通Button按键
		BUTTON_NORMAL,
		// 普通ImageButton按键
		IMAGE_BUTTON_NORMAL,
		// 自定义按键
		CUSTOM_BUTTON,
	}
}
